var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];
var shuzuq = [];
var moreData = "";
function getAllDCdateBuType() {
    var aa = sessionStorage.getItem("customerid")
    $.ajax({
        url: root.dockingplatform.queryWarehouseCityMap,
        cache: false, //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            customerId: sessionStorage.getItem("customerid"),
        }),
        success: function(result){
            if(result.errorCode=="0" || result.errorCode=="4"){
                moreData = result.data;
                for(var i = 0; i<result.data.warehouseList.length; i++){
                    shuzuq.push(result.data.warehouseList[i].warehouseId);
                }
                goodsList(result);
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
    // var result = {
    //     "data":{
    //
    //         "customerId":"222",
    //         "warehouseList":[
    //             {
    //
    //                 "state":0,
    //                 "warehouseId":2,
    //                 "warehouseName":"天津仓",
    //                 "warehouseCoverAddress":[
    //                     {
    //                         "code":3,
    //                         "name":"天津",
    //                         "secondAddressLibrary":[
    //                             {"code":51035,
    //                                 "name":"东丽区"},
    //                             {"code":51037,
    //                                 "name":"河北区"}
    //                         ]
    //                     },
    //                     {
    //                         "code":7,
    //                         "name":"河南",
    //                         "secondAddressLibrary":[
    //                             {"code":517,
    //                                 "name":"商丘市"},
    //                             {"code":527,
    //                                 "name":"周口市"},
    //                             {"code":468,
    //                                 "name":"安阳市"}
    //                         ]
    //                     }
    //                 ],
    //             }
    //         ]
    //     },
    //     "errorMessage":"成功",
    //     "errorCode":0
    // }
    // moreData = result.data;
    // goodsList(result);
}

/*账户信息编辑*/
function spu_edit(title, url, row) {
    /*删除添加spu图片的缓存，防止刷新页面后缓存不消失*/
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

function channelDetails2(title,url,data,ids,state){

    title
    url
    data
    ids
    state
    sessionStorage.removeItem("click_a")
    sessionStorage.removeItem("click_b")
    sessionStorage.removeItem("zhekoulv")

    if(state != 0){
        return layer.msg("无效状态下的配置不能修改", {icon: 5,time:1000});
    }
    sessionStorage.setItem("state",state);
    sessionStorage.setItem("id",ids);
    sessionStorage.setItem("customerid",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}


function goodsList(dataList){
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }
    var customerId = dataList.data.customerId;

    if(dataList.data != null){
        $.each(dataList.data.warehouseList, function(i, field, row) {
                var rowData = new Array();
                rowData.push(field.warehouseName);
                rowData.push(field.warehouseCoverAddress);
            console.log(customerId);
            console.log(field.state);
                if (field.state == 0) {
                    rowData.push('<a onclick="brand_stop(\'\',\'' + customerId +'\',\''+ field.warehouseId+ '\')">有效</a>');
                } else {
                    rowData.push('<a onclick="brand_start(\'\',\'' +  customerId +'\',\''+ field.warehouseId + '\')">无效</a>');
                }
                rowData.push('<a onclick="channelDetails2(\'修改\',\'transportWay-update.html\',\'' +  customerId +'\',\''+ field.warehouseId + '\',\'' +  field.state +'\')">修改</a>')
                dataSource.push(rowData);
            rowData.push(field.warehouseId);
        });
    }
    rangeDataTable(dataSource);
    function rangeDataTable(dataSource) {
        var dttable = $('#warehouseTable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#warehouseTable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row, meta) {
                        var dataAll = row[1];
                        console.log("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+row[0]);
                        console.log("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+row[1]);
                        console.log("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+row[2]);
                        console.log("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+row[3]);
                        console.log("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+row[4]);


                        var ulTop = '<ul class="radius text-l" id="ulList">';
                        var list="";
                        var ulfoot ='</ul><a class="edit powerButton 131" style="float: right;" onclick="area(\'修改覆盖范围\',this,\''+row[5]+'\',\''+row[0]+'\')"></a>';
                        for(var i=0;i<dataAll.length;i++) {
                            //console.log(dataAll[i].child_address_library)
                            if (dataAll[i].secondAddressLibrary.length == 0){
                                list += '<li class="f-l ml-10 liList" id="' + dataAll[i].code + '"><span class="name">' + dataAll[i].name2 + '全境</span><sup style="color:#ff1b1b; cursor:pointer;">' +
                                    '<b onclick="remove(this,' + row[5] + ',' + row[0] + ')">x</b></sup></li>'
                            }else{
                                list += '<li class="f-l ml-10 liList" id="' + dataAll[i].code + '"><span class="name">' + dataAll[i].name + '<a id="'+dataAll[i].code+'" class="powerButton 130" onclick="more(this,'+row[4]+')"> 更多</a></span><sup style="color:#ff1b1b; cursor:pointer;">'
                            }

                        }
                        ulTop += list + ulfoot;
                        return ulTop;
                    },
                    "targets": 1
                },
                // {
                //     "render": function(data, type, row, meta) {
                //         if(row[2] == 10){
                //             var val = '<span class="dropDown"> <a class="dropDown_A powerButton 411" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">启动<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="brand_stop(\'\',\''+ row[4] +'\')"><a href="#">启动</a></li></ul></span>';
                //             return val;
                //         }else{
                //             var val = '<span class="dropDown"> <a class="dropDown_A powerButton 411" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">禁用<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="brand_start(\'\',\''+ row[4] +'\')"><a href="#">禁用</a></li></ul></span>';
                //             return val;
                //         }
                //     },
                //     "targets": 2
                // },
                //
                // {
                //     "render": function(data, type, row, meta) {
                //         var val = '<a onclick="channelDetails2(\'修改\',\'transportWay-update.html\')"><span id="areaList" class="list">修改</span></a>';
                //         return val;
                //     },
                //     "targets": 3
                // },
            ]
        });
        //权限控制函数
        power()
    };
}

function more(thil,warhouseCode) {

    var allCity = "";
    for(var i=0;i<moreData.warehouseList.length;i++) {
        if (warhouseCode == moreData.warehouseList[i].warehouseId){
            var cityList = moreData.warehouseList[i].warehouseCoverAddress;
            for(var j=0;j<cityList.length;j++) {
                if (thil.id == cityList[j].code){
                    allCity = cityList[j].secondAddressLibrary;
                }
            }


        }
    }

    var cityName = "";
    for(var i=0;i<allCity.length;i++) {
        if(allCity[i].thirdAddressLibrary.length==0){
            cityName +=  allCity[i].name+'全境,'
        }else {
            for(var k=0;k<allCity[i].thirdAddressLibrary.length;k++) {

                cityName += allCity[i].name + allCity[i].thirdAddressLibrary[k].name+','
            }
        }

    }
    layer.confirm(cityName, {
        btn: ['返回'] //按钮
    }, function(index,layero){
        layer.close(index);
        //确定执行方法
    });

}



function relation_add(title,url){
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}



/*停用*/
function brand_stop(obj,customerId,warehouseId){

    customerId
    warehouseId
    layer.confirm('确认要停用吗？',function(index){
        $.ajax({
            type: "POST",
            url: root.dockingplatform.updateWarehouseCityMapState,
            cache: false, //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data: JSON.stringify({
                customerId : customerId,
                warehouseId: warehouseId,
                state:1
            }),
            success: function (data) {
                switch (data.errorCode) {
                    case 0:
                        layer.msg("停用成功", {icon: 2, time: 2000});
                        window.location.reload();
                }
            },
        });
        layer.msg('已停用!', {icon: 6,time:1000});
    });
}
/*启用*/
function brand_start(obj,customerId,warehouseId){
    customerId
    warehouseId
    layer.confirm('确认要启用吗？',function(index){
        $.ajax({
            type: "POST",
            url: root.dockingplatform.updateWarehouseCityMapState,
                cache: false, //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data: JSON.stringify({
                    customerId : customerId,
                    warehouseId: warehouseId,
                    state:0
                }),
            //传入组装的参数
            success: function (data) {
                switch (data.errorCode) {
                    case 0:
                        layer.msg("启动成功", {icon: 2, time: 2000});
                        window.location.reload();
                }
            },
        });
        layer.msg('已启用!', {icon: 6,time:1000});
    });
}